﻿using ShapeDrawer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace CircleDrawer.Shapes
{
    public abstract class ShapeSD : IDeletable
    {
        private double width;
        private double height;
        private Thickness margin;
        private Brush stroke;
        private double strokeThickness;

        protected Shape wrapper;
        protected Shape visibleShape;

        protected void InitializeEvents()
        {
            this.wrapper.Stroke = new SolidColorBrush(Colors.Transparent);
            this.wrapper.MouseLeftButtonUp += WrapperMouseClick;
            this.wrapper.MouseEnter += FrameHoverIn;
            this.wrapper.MouseLeave += FrameHoverOut;
        }

        void FrameHoverOut(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (ShapeDrawing.Mode == DrawingMode.Delete)
            {
                this.visibleShape.StrokeThickness--;
                this.visibleShape.Stroke = this.stroke;
                this.wrapper.Stroke = new SolidColorBrush(Colors.Transparent);
            }
        }

        void FrameHoverIn(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (ShapeDrawing.Mode == DrawingMode.Delete)
            {
                this.visibleShape.StrokeThickness++;
                this.visibleShape.Stroke = new SolidColorBrush(Colors.Red);
                this.wrapper.Stroke = new SolidColorBrush(Color.FromArgb(60, 180, 180, 180));
            }
        }

        void WrapperMouseClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (ShapeDrawing.Mode == DrawingMode.Delete)
            {
                this.Delete();
            }
        }

        public double Width
        {
            get
            {
                return this.width;
            }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("Width can't be negative");
                }
                this.visibleShape.Width = value;
                this.wrapper.Width = value + 12;

                this.width = value;
            }
        }

        public double Height
        {
            get
            {
                return this.height;
            }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("Height can't be negative");
                }
                this.visibleShape.Height = value;
                this.wrapper.Height = value + 12;

                this.height = value;
            }
        }

        public Thickness Margin
        {
            get
            {
                return this.margin;
            }
            set
            {
                this.visibleShape.Margin = value;
                double leftWrapper = value.Left - 6;
                double topWrapper = value.Top - 6;
                this.wrapper.Margin = new Thickness(leftWrapper, topWrapper, value.Right, value.Bottom);

                this.margin = value;
            }
        }

        public Brush Stroke
        {
            get
            {
                return this.stroke;
            }
            set
            {
                this.visibleShape.Stroke = value;
                this.stroke = value;
            }
        }

        public double StrokeThickness
        {
            get
            {
                return this.strokeThickness;
            }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("Stroke Thickness can't be negative");
                }
                this.visibleShape.StrokeThickness = value;
                this.wrapper.StrokeThickness = value + 12;

                this.strokeThickness = value;
            }
        }

        public virtual void Delete()
        {
            Panel container = this.visibleShape.Parent as Panel;
            container.Children.Remove(this.visibleShape);
            container.Children.Remove(this.wrapper);
        }

        public void DrawIn(Panel container)
        {
            container.Children.Add(this.visibleShape);
            container.Children.Add(this.wrapper);
        }
    }
}
